package com.cqtd.common.dict.action;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import com.cqtd.common.util.StaticMethod;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.cqtd.common.base.action.FooGenericAction;
import com.cqtd.common.base.utils.FooUtils;
import com.cqtd.common.dict.model.CqSystemDictType;
import com.cqtd.common.dict.service.CqSystemDictTypeService;
import com.cqtd.common.dict.ui.CqDictHelper;
import com.cqtd.common.dict.ui.CqDictTree;
import com.google.common.collect.Lists;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;

public class CqSystemDictTypeAction extends FooGenericAction 
									implements ModelDriven<CqSystemDictType>,Preparable{
	protected Logger logger = LoggerFactory.getLogger(this.getClass().getName());
	private static final long serialVersionUID = 1L;
	@Autowired
	private CqSystemDictTypeService cqSystemDictTypeService ;
	private CqSystemDictType cqSystemDictType = new CqSystemDictType();
	
	public void getDictTree() throws IOException {
		String dictId  = StaticMethod.nullObject2String(request.getParameter("dictId"));
		List<CqSystemDictType> dictList = Lists.newArrayList();
		List<CqDictTree> cdDictTreeList = Lists.newArrayList();
		if("".equals(dictId)){
			dictList = cqSystemDictTypeService.getDictByParentDictId("1");
		}else{
			dictList = cqSystemDictTypeService.getDictByParentDictId(dictId);
		}
		if(dictList!=null && dictList.size()>0){
			try {
				cdDictTreeList = CqDictHelper.copyProperties(dictList);
			} catch (IllegalAccessException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (InvocationTargetException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		FooUtils.printJsonObjectSerializeNulls(response, cdDictTreeList);
	}
	
	public void saveDict() throws IOException {
		try{	
			String dictId  = StaticMethod.nullObject2String(request.getParameter("dictId"));
		if(dictId.equals("")){
			String currentDictId  = StaticMethod.nullObject2String(request.getParameter("currentDictId"));
			List<CqSystemDictType> dictList = cqSystemDictTypeService.getDictByParentDictId(currentDictId);
			if(dictList!=null && dictList.size()>0){
				cqSystemDictType.setDictId(currentDictId +"0"+ (dictList.size()+1));
				cqSystemDictType.setDictCode(currentDictId +"0"+ (dictList.size()+1));
				cqSystemDictType.setParentDictId(currentDictId);
				cqSystemDictType.setLeaf("false");
				cqSystemDictType.setDeleted("0");
				cqSystemDictTypeService.saveCqSystemDictType(cqSystemDictType);
			}else{
				CqSystemDictType parentCqSystemDictType = cqSystemDictTypeService.getDictByDictId(currentDictId);
				parentCqSystemDictType.setLeaf("true");
				cqSystemDictType.setDictId(currentDictId+"01");
				cqSystemDictType.setDictCode(currentDictId+"01");
				cqSystemDictType.setParentDictId(currentDictId);
				cqSystemDictType.setLeaf("false");
				cqSystemDictType.setDeleted("0");
				cqSystemDictTypeService.saveCqSystemDictType(parentCqSystemDictType,cqSystemDictType);
			}
		}else{
			CqSystemDictType cqSystemDictTypeDB = cqSystemDictTypeService.getDictByDictId(dictId);
			cqSystemDictTypeDB.setDictName(cqSystemDictType.getDictName());
			cqSystemDictTypeDB.setDictRemark(cqSystemDictType.getDictRemark());
			cqSystemDictTypeDB.setSort(cqSystemDictType.getSort());
			cqSystemDictTypeDB.setModuleName(cqSystemDictType.getModuleName());
			cqSystemDictTypeService.saveCqSystemDictType(cqSystemDictTypeDB);	
		}
		}catch (Exception e) {
			e.printStackTrace();
			System.out.println("============"+e.getMessage());
		}
		
		
		FooUtils.printJsonSuccessMsg(response);
	}

	public void removeDict() throws IOException {
		String currentDictId  = StaticMethod.nullObject2String(request.getParameter("currentDictId"));
		List<CqSystemDictType> dictList = cqSystemDictTypeService.getDictByParentDictIdWithLike(currentDictId);
		if(dictList!=null && dictList.size()>0){
			cqSystemDictTypeService.remove(dictList);
		}
		CqSystemDictType cqSystemDictTypeDB = cqSystemDictTypeService.getDictByDictId(currentDictId);
		if(cqSystemDictTypeDB!=null){
			cqSystemDictTypeService.remove(cqSystemDictTypeDB);
		}
		FooUtils.printJsonSuccessMsg(response);
	}
	
	
	public void prepare() throws Exception {
		// TODO Auto-generated method stub
		
	}

	public CqSystemDictType getModel() {
		// TODO Auto-generated method stub
		return cqSystemDictType;
	}

	public CqSystemDictType getCqSystemDictType() {
		return cqSystemDictType;
	}

	public void setCqSystemDictType(CqSystemDictType cqSystemDictType) {
		this.cqSystemDictType = cqSystemDictType;
	}
	
	
}
